<?php

namespace Message;

use C\L\AdmController;
use C\M\Message;
use C\M\User;

class MessageController extends AdmController
{
    protected function init()
    {
        $this->service = $this->s_message;
    }

    public function indexAction()
    {
        $currentPage = $this->request->getQuery('page', 'int', 1);
        $perPage     = $this->request->getQuery('per_page', 'int', 20);
        $mobile      = $this->request->getQuery('mobile', 'string', null);

        $builder = $this
        ->modelsManager
        ->createBuilder()
        ->columns([
        'messages.*',
        'users.name AS user_name',
        'users.mobile AS user_mobile',
        'levels.name as level_name',
        ])
        ->from(['messages' => 'C\M\Message'])
        ->leftJoin(
            'C\M\User',
            'users.uid = messages.messageable_id',
            'users'
        )
        ->leftJoin(
            'C\M\UserLevel',
            'levels.id = messages.messageable_id',
            'levels'
        )
        ->orderBy('messages.id DESC');

        if ($mobile) {
            $builder = $builder->andWhere(
                "messages.messageable_type = 'user' AND users.mobile = :mobile:",
                ['mobile' => $mobile]
            );
        }

        $result = $this->paginationByBuilder($builder, $currentPage, $perPage);

        return $this->success($result);
    }

    public function createAction()
    {
        $title           = $this->request->getPost('title');
        $content         = $this->request->getPost('content');
        $messageableType = $this->request->getPost('messageable_type');
        $levelId         = $this->request->getPost('level_id', 'int', null);
        $mobile          = $this->request->getPost('mobile');

        $messageableId = null;
        switch ($messageableType) {
            case 'user':
                $user = User::findByMobile($mobile);
                if (!$user) {
                    return $this->error('用户不存在');
                }
                $messageableId = $user->uid;
                break;
            case 'level':
                $messageableId = $levelId;
            default:
                break;
        }

        $this->s_message->create($messageableId, $messageableType, $title, $content)
        ? $this->success()
        : $this->error();
    }

    public function showAction()
    {
        $id = $this->request->getQuery('id', 'int', 0);

        $message = Message::findById($id);
        if (!$message) {
            return $this->error('未找到该站内信');
        }

        return $this->success($message->toArray());
    }

    public function deleteAction()
    {
        $id = $this->request->getPost('id', 'int', null);

        return $this->s_message->delete($id)
        ? $this->success()
        : $this->error();
    }

    public function updateAction()
    {
        $id              = $this->request->getPost('id', 'int', null);
        $title           = $this->request->getPost('title');
        $content         = $this->request->getPost('content');
        $messageableType = $this->request->getPost('messageable_type');
        $levelId         = $this->request->getPost('level_id', 'int', null);
        $mobile          = $this->request->getPost('mobile');

        $messageableId = null;
        switch ($messageableType) {
            case 'user':
                $user = User::findByMobile($mobile);
                if (!$user) {
                    return $this->error('用户不存在');
                }
                $messageableId = $user->uid;
                break;
            case 'level':
                $messageableId = $levelId;
            default:
                break;
        }

        $this->s_message->newUpdate($id, $messageableId, $messageableType, $title, $content)
        ? $this->success()
        : $this->error();
    }
}
